package PTOP.ptopjms;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import javax.jms.*;

public class Consumer {

    public static String BROKER_URL = "tcp://0.0.0.0:61616";
    public static String USER = "admin";
    public static String PASSWORD = "admin";
    public static String DESITNATION = "SystemA.SystemB.Queue";
    public static void main(String[] args) throws JMSException {
        //创建连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
        //创建连接
        Connection connection = factory.createConnection(USER,PASSWORD);
        connection.start();
        //创建会话
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //消息目的地
        Destination destination = new ActiveMQQueue(DESITNATION);
        //创建消费者
        MessageConsumer messageConsumer = session.createConsumer(destination);
        //阻塞方法，接收消息
        Message msg = messageConsumer.receive();
        if(msg!=null){
            if(msg instanceof TextMessage){
                String s = ((TextMessage) msg).getText();
                System.out.println(s);
            }else{
                System.out.println("no message  Type");
            }
            msg.acknowledge();
        }else {
            System.out.println("no message!");
        }
        //释放资源
        session.close();
        connection.close();
        System.exit(0);
    }
}
